﻿using EM.FrameWork.Paging;
using EM.OA.BLL;
using EM.OA.DataModel.DBModel;
using EM.OA.Portal.Models;
using EM.OA.WebCommon;
using EM.OA.WebCommon.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EM.OA.Portal.Controllers
{
    public class KnowledgeController : BaseAuthController
    {
         /// <summary>
        /// 返回Index视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
  
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="rows">每页条数</param>
        /// <returns></returns>
        public JsonResult GetList(string page, string rows)
        {
            KnowledgeBLL bll = new KnowledgeBLL();
            string knowledgeName = GetParam("KnowledgeName");
            string knowledgeKind = GetParam("DictionaryKind");
            List<object> result = new List<object>();
            List<EMOA_Knowledge> list = new List<EMOA_Knowledge>();
            list = bll.GetPagedList(Convert.ToInt32(page), Convert.ToInt32(rows), p => (knowledgeName.IsNullOrEmpty() || p.KnowledgeName == knowledgeName) && (knowledgeKind.IsNullOrEmpty() || p.DictionaryKind == knowledgeKind));
            list.ForEach(p =>
            {
                result.Add(new KnowledgeModels().ToModel<EMOA_Knowledge>(p));
            });

            return ToJson(result, list.Count());
        }

        /// <summary>
        /// 增加实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(KnowledgeModels model)
        {
            try
            {
                //if (ModelState.IsValid)
                //{
                    KnowledgeBLL bll = new KnowledgeBLL();
                    EMOA_Knowledge entity = model.ToEntity<EMOA_Knowledge>();
                    entity.KnowledgeID = Guid.NewGuid();
                    entity.CreateBy = CurrentUser.UserID;
                    entity.CreateTime = DateTime.Now;                    
		if (bll.Add(entity))
                    {
                        return Content("OK");
                    }
                    else
                    {
                        return Content("增加失败");
                    }
                //}
                //else
                //    return Content("验证失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(KnowledgeModels model)
        {
            try
            {
                //if (ModelState.IsValid)
                //{
                    KnowledgeBLL bll = new KnowledgeBLL();
                    EMOA_Knowledge entity = bll.GetEntity(model.KnowledgeID);
                    entity = model.ToEntity<EMOA_Knowledge>(entity);
                    entity.UpdateBy = CurrentUser.UserID;
                    entity.UpdateTime = DateTime.Now;                    
		if (bll.Update(entity))
                        return Content("OK");
                    else
                        return Content("修改失败");
                //}
                //else
                //{
                //    return Content("验证失败");
                //}
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(string id)
        {
            try
            {
                KnowledgeBLL bll = new KnowledgeBLL();
                if (bll.Delete(Guid.Parse(id)))
                    return Content("OK");
                else
                    return Content("删除失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

    }
}